package com.fengze.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.fengze.business.mapper.ContractInfoMapper;
import com.fengze.business.mapper.ProjectInfoMapper;
import com.fengze.common.annotation.AutoSetInsertValue;
import com.fengze.common.annotation.AutoSetUpdateValue;
import com.fengze.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fengze.business.mapper.ReceivePaymentMapper;
import com.fengze.business.domain.ReceivePayment;
import com.fengze.business.service.IReceivePaymentService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 收款Service业务层处理
 * 
 * @author lug
 * @date 2024-12-10
 */
@Service
public class ReceivePaymentServiceImpl implements IReceivePaymentService 
{
    @Resource
    private ReceivePaymentMapper receivePaymentMapper;

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Resource
    private ContractInfoMapper contractInfoMapper;

    /**
     * 查询收款
     * 
     * @param id 收款主键
     * @return 收款
     */
    @Override
    public ReceivePayment selectReceivePaymentById(Long id)
    {
        return receivePaymentMapper.selectReceivePaymentById(id);
    }

    /**
     * 查询收款列表
     * 
     * @param receivePayment 收款
     * @return 收款
     */
    @Override
    public List<ReceivePayment> selectReceivePaymentList(ReceivePayment receivePayment)
    {
        return receivePaymentMapper.selectReceivePaymentList(receivePayment);
    }

    /**
     * 新增收款
     * 
     * @param receivePayment 收款
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @AutoSetInsertValue
    @Override
    public int insertReceivePayment(ReceivePayment receivePayment)
    {
        int successCount = receivePaymentMapper.insertReceivePayment(receivePayment);
        if(receivePayment.getProjectId() != null){
            BigDecimal receiveMoney = receivePaymentMapper.sumByProjectId(receivePayment.getProjectId());
            projectInfoMapper.updateReceiveMoney(receivePayment.getProjectId(),receiveMoney);
        }

        if(receivePayment.getContractId() != null){
            BigDecimal receiveMoney = receivePaymentMapper.sumByContractId(receivePayment.getContractId());
            contractInfoMapper.updateReceiveMoney(receivePayment.getContractId(),receiveMoney);
        }

        return successCount;
    }

    /**
     * 修改收款
     * 
     * @param receivePayment 收款
     * @return 结果
     */
    @AutoSetUpdateValue
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateReceivePayment(ReceivePayment receivePayment)
    {
        int successCount =  receivePaymentMapper.updateReceivePayment(receivePayment);

        if(receivePayment.getProjectId() != null){
            BigDecimal receiveMoney = receivePaymentMapper.sumByProjectId(receivePayment.getProjectId());
            projectInfoMapper.updateReceiveMoney(receivePayment.getProjectId(),receiveMoney);
        }

        if(receivePayment.getContractId() != null){
            BigDecimal receiveMoney = receivePaymentMapper.sumByContractId(receivePayment.getContractId());
            contractInfoMapper.updateReceiveMoney(receivePayment.getContractId(),receiveMoney);
        }

        return successCount;
    }

    /**
     * 批量删除收款
     * 
     * @param ids 需要删除的收款主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteReceivePaymentByIds(Long[] ids)
    {
        int sum = 0;
        for (Long id : ids) {
            ReceivePayment receivePayment = receivePaymentMapper.selectReceivePaymentById(id);
            if(receivePayment.getProjectId() != null){
                BigDecimal receiveMoney = receivePaymentMapper.sumByProjectId(receivePayment.getProjectId());
                projectInfoMapper.updateReceiveMoney(receivePayment.getProjectId(),receiveMoney);
            }

            if(receivePayment.getContractId() != null){
                BigDecimal receiveMoney = receivePaymentMapper.sumByContractId(receivePayment.getContractId());
                contractInfoMapper.updateReceiveMoney(receivePayment.getContractId(),receiveMoney);
            }


            int successCount =  receivePaymentMapper.deleteReceivePaymentById(id);
            sum += successCount;
        }

        return sum;
    }

    /**
     * 删除收款信息
     * 
     * @param id 收款主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteReceivePaymentById(Long id)
    {
        ReceivePayment receivePayment = receivePaymentMapper.selectReceivePaymentById(id);

        if(receivePayment.getProjectId() != null){
            BigDecimal receiveMoney = receivePaymentMapper.sumByProjectId(receivePayment.getProjectId());
            projectInfoMapper.updateReceiveMoney(receivePayment.getProjectId(),receiveMoney);
        }

        if(receivePayment.getContractId() != null){
            BigDecimal receiveMoney = receivePaymentMapper.sumByContractId(receivePayment.getContractId());
            contractInfoMapper.updateReceiveMoney(receivePayment.getContractId(),receiveMoney);
        }


        int successCount =  receivePaymentMapper.deleteReceivePaymentById(id);
        return successCount;
    }
}
